const http = require('http')

const server = http.createServer()

server.on('request',(req,res)=>{

   console.log('someone visit the server!');
   
   let content = '404 not found!'

   if(req.url === '/' || req.url === '/index.html'){
       content = '<h2>首页</h2>'
   }else if(req.url === '/about.html'){
       content = '<h2>关于页面</h2>'
   }

   // (4) 为了防止客户端乱码，要在响应头上设置编码格式 ，即内容类型为文本，且文本时utf-8格式
   res.setHeader('Content-Type','text/html;charset=utf-8')

   // (3) 向客户端发送响应数据，并结束请求(你要的全拿走，拿走我就关)
   res.end(content)  
})

// 4.启动服务器
server.listen(8080,()=>{
   console.log('The server is running at http://127.0.0.1:8080');
})

/**
 * 只要有人访问http://127.0.0.1:8080，那么就会打印 someone visit the server!
*/